package com.james.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.james.constant.MessageConstant;
import com.james.dao.MemberDao;
import com.james.dao.OrderDao;
import com.james.dao.OrderSettingDao;
import com.james.entity.Result;
import com.james.pojo.Member;
import com.james.pojo.Order;
import com.james.pojo.OrderSetting;
import com.james.service.OrderService;
import com.james.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ACT
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Override
    public Result order(Map<String, Object> map) throws Exception {
        //1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (null == orderSetting) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        //可预约人数
        int number = orderSetting.getNumber();
        //以预约人数
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
        //用户手机号
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if (null != member) {
            //获取用户的id
            Integer memberId = member.getId();
            //获取套餐id
            Integer setmealId = Integer.parseInt((String) map.get("setmealId"));
            Order order = new Order(memberId, date, memberId);
            //根据条件进行查询
            //有bug，数据库查询不出结果
            List<Order> list = orderDao.findByCondition(order);
            if (null != list && list.size() > 0) {
                //用户在重复预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            //自动完成会员注册
            memberDao.add(member);
        }
        //5、预约成功，更新当日的已预约人数
        Order order = new Order();
        //设置会员ID
        order.setMemberId(member.getId());
        //预约日期
        order.setOrderDate(date);
        //预约类型
        order.setOrderType((String) map.get("orderType"));
        //到诊状态
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //套餐ID
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        //添加预约信息
        orderDao.add(order);
        //设置已预约人数+1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        //修改预约设置
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Map<String, Object> findById(Integer id) throws Exception {
        Map<String, Object> map = orderDao.findById4Detail(id);
        if (null != map) {
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
